<script lang="ts" setup name="Staff">
import { CirclePlus, Delete, EditPen } from '@element-plus/icons-vue'
import { cloneDeep } from 'lodash-es'
import { ProTableInstance } from '~/library/components/ProTable/interface'
import { StoreManagement } from '~/src/api/interface/storeManagement'
import { createStaffInfoAPI, deleteStaffInfoAPI, editStaffInfoAPI, getDepartmentAPI, getStaffInfoAPI } from '~/src/api/storeManagement'
import { staffColumn } from '../columns'
import StaffDialog from '../components/StaffDialog.vue'
const defaultDta = reactive<StoreManagement.StaffInfo>({
  departmentId: 0,
  mobile: '',
  name: '',
  sex: 1,
})
const proTableRef = ref<ProTableInstance>()
const dialogRef = ref<InstanceType<typeof StaffDialog>>()
const openDrawer = async (title: string, data: StoreManagement.StaffInfo) => {
  const res = await getDepartmentAPI({ pageNum: 1, pageSize: 20 })
  data.id ? null : (defaultDta.departmentId = res.data.list[0].id as number)
  dialogRef.value!.acceptParams({
    title,
    data: cloneDeep(data),
    api: data.id ? editStaffInfoAPI : createStaffInfoAPI,
    getTableList: proTableRef.value!.getTableList,
    list: cloneDeep(res.data.list),
  })
}
const deleteAccount = () => {
  useDeleteData(deleteStaffInfoAPI, proTableRef.value as ProTableInstance)
}
</script>

<template>
  <div class="auto-height-container">
    <pro-table ref="proTableRef" :columns="staffColumn" :request-api="getStaffInfoAPI" title="员工列表">
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button v-auth="'add'" :icon="CirclePlus" type="primary" @click="openDrawer('新增', defaultDta)">新增员工</el-button>
        <el-button v-auth="'delete'" :icon="Delete" @click="deleteAccount">删除</el-button>
      </template>
      <!-- Expand -->
      <template #expand="scope">
        {{ scope.row }}
      </template>
      <!-- 表格操作 -->
      <template #name="{ row }">
        <!-- <el-button v-auth="'view'" :icon="View" link type="primary" @click="openDrawer('查看', row)">查看</el-button> -->
        <el-button v-auth="'edit'" :icon="EditPen" link type="primary" @click="openDrawer('编辑', row)">{{ row.name }}</el-button>
      </template>
    </pro-table>
    <staff-dialog ref="dialogRef" />
  </div>
</template>

<style lang="scss" scoped></style>
